package com.baihan.angus.app.game.busshow.ui.user.edit;

import android.os.Bundle;
import android.text.TextUtils;

import androidx.lifecycle.Observer;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.baihan.angus.app.game.basic.base.BaseActivity;
import com.baihan.angus.app.game.basic.utils.Tooast;
import com.baihan.angus.app.game.busshow.BR;
import com.baihan.angus.app.game.busshow.R;
import com.baihan.angus.app.game.busshow.databinding.ActivityEditNickNameBinding;
import com.baihan.angus.app.game.common.arouter_path.ArouterAppPath;
import com.baihan.angus.app.game.common.bean.response.Userinfo;
import com.baihan.angus.app.game.common.configs.UserConfig;

import java.util.HashMap;
import java.util.Map;

/**
 * 编辑昵称
 */
@Route(path = ArouterAppPath.EDIT_NICK_NAME_ACTIVITY)
public class EditNickNameActivity extends BaseActivity<ActivityEditNickNameBinding, EditNickNameVM> {

    @Autowired(name = "userinfo")
    Userinfo userinfo;

    private Map<String, Object> mModifyRequest = new HashMap<>();

    @Override
    protected int getContentLayout() {
        return R.layout.activity_edit_nick_name;
    }

    @Override
    protected int getViewModelId() {
        return BR.editNickNameVM;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {

        mVDBinding.titleView.setLeftOnClickListener(view -> finish());

        mVDBinding.titleView.setRightOnClickListener(view -> {
            if (!TextUtils.isEmpty(mVDBinding.etNickName.getText())) {
                mModifyRequest.put("avatar", userinfo.getAvatar());
                mModifyRequest.put("gender", userinfo.getGender());
                mModifyRequest.put("nickname", mVDBinding.etNickName.getText().toString());
                mModifyRequest.put("bio", userinfo.getBio());
                mViewModel.modifyNickName(mModifyRequest);
            } else {
                Tooast.normalInfo(getString(R.string.enterNickName));
            }
        });

        mVDBinding.etNickName.setText(userinfo.getNickname());

        mVDBinding.ivClear.setOnClickListener(view -> {
            mVDBinding.etNickName.setText("");
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initObservers() {
        super.initObservers();
        mViewModel.mldModifyResult.observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                if (aBoolean) {
                    userinfo.setNickname(mVDBinding.etNickName.getText().toString());
                    UserConfig.getUserConfig().saveUser(userinfo);
                    Tooast.normalInfo(getString(R.string.modifySuccess));
                    finish();
                }
            }
        });
    }
}